package com.kelin.mvvmlight.bindingadapter.image;

//import android.databinding.adapters.ImageViewBindingAdapter

import android.content.res.Resources;
import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.kelin.mvvmlight.util.BitmapUtil;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by kelin on 16-3-24.
 */
public final class ViewBindingAdapter {

    //    @BindingAdapter({"uri"})
//    public static void setImageUri(SimpleDraweeView simpleDraweeView, String uri) {
//        if (!TextUtils.isEmpty(uri)) {
//            simpleDraweeView.setImageURI(Uri.parse(uri));
//        }
//    }
    @BindingAdapter(value = {"placeholderDrawable"})
    public static void setImageResourse(final ImageView imageView, @DrawableRes int drawable) {
        imageView.setImageResource(drawable);
    }

    @BindingAdapter({"android:src"})
    public static void setImageViewResource(ImageView imageView, int resource) {
        imageView.setImageResource(resource);
    }

    @BindingAdapter(value = {"url"})
    public static void setImageUrl(final ImageView imageView, String url) {
//        if (!TextUtils.isEmpty(url)) {
        Glide.with(imageView.getContext()).load(url).into(imageView);
//        }
    }



    @BindingAdapter(value = {"url", "placeholderDrawable"})
    public static void setImageUrlWithHolders(final ImageView imageView, String url, Drawable placeholderDrawable) {
        Glide.with(imageView.getContext()).load(url).placeholder(placeholderDrawable).into(imageView);
    }

    @BindingAdapter(value = {"placeholderImageRes"})
    public static void setImageUrlWithHolders(final ImageView imageView, @DrawableRes int placeholderImageRes) {
        Glide.with(imageView.getContext()).load(placeholderImageRes).placeholder(placeholderImageRes).into(imageView);
    }

    @BindingAdapter(value = {"url", "placeholderImageRes"})
    public static void setImageUrlWithHolders(final ImageView imageView, String url, @DrawableRes int placeholderImageRes) {
        Glide.with(imageView.getContext()).load(url).placeholder(placeholderImageRes).into(imageView);
    }

    @BindingAdapter(value = {"url", "placeholderDrawable", "circle"})
    public static void setCircleImageUrlWithHolders(final ImageView imageView, String url, Drawable placeholderDrawable, boolean isCircle) {
        if (isCircle) {
            Glide.with(imageView.getContext()).load(url).bitmapTransform(new CropCircleTransformation(imageView.getContext()))
                    .placeholder(BitmapUtil.INSTANCE.getCircleRes(imageView.getContext(), placeholderDrawable))
                    .into(imageView);
//            Glide.with(imageView.getContext()).load(url).placeholder(android.R.drawable.ic_delete).into(imageView);
        } else {
            Glide.with(imageView.getContext()).load(url).placeholder(placeholderDrawable).into(imageView);
        }
    }

    @BindingAdapter(value = {"url", "placeholderImageRes", "circle"})
    public static void setCircleImageUrlWithHolders(final ImageView imageView, String url, @DrawableRes int placeholderImageRes, boolean isCircle) {
        if (isCircle) {
            Glide.with(imageView.getContext()).load(url).bitmapTransform(new CropCircleTransformation(imageView.getContext()))
                    .placeholder(BitmapUtil.INSTANCE.getCircleRes(imageView.getContext(), placeholderImageRes))
                    .into(imageView);
//            Glide.with(imageView.getContext()).load(url).placeholder(android.R.drawable.ic_delete).into(imageView);
        } else {
            Glide.with(imageView.getContext()).load(url).placeholder(placeholderImageRes).into(imageView);
        }
    }

    @BindingAdapter(value = {"progressDrawable"})
    public static void setprogressDrawable(final ProgressBar progressBar, int res) {
        Resources resources = progressBar.getContext().getResources();
        progressBar.setProgressDrawable(resources.getDrawable(res));
    }
//
//    @BindingAdapter(value = {"uri", "placeholderImageRes", "request_width", "request_height", "onSuccessCommand", "onFailureCommand"}, requireAll = false)
//    public static void loadImage(final ImageView imageView, String uri,
//                                 @DrawableRes int placeholderImageRes,
//                                 int width, int height,
//                                 final ReplyCommand<Bitmap> onSuccessCommand,
//                                 final ReplyCommand<DataSource<CloseableReference<CloseableImage>>> onFailureCommand) {
//
//        imageView.setImageResource(placeholderImageRes);
//        if (!TextUtils.isEmpty(uri)) {
//            ImagePipeline imagePipeline = Fresco.getImagePipeline();
//            ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri));
//            if (width > 0 && height > 0) {
//                builder.setResizeOptions(new ResizeOptions(width, height));
//            }
//            ImageRequest request = builder.build();
//            DataSource<CloseableReference<CloseableImage>>
//                    dataSource = imagePipeline.fetchDecodedImage(request, imageView.getContext());
//            dataSource.subscribe(new BaseBitmapDataSubscriber() {
//                @Override
//                protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
//                    if (onFailureCommand != null) {
//                        onFailureCommand.execute(dataSource);
//                    }
//                }
//
//                @Override
//                protected void onNewResultImpl(Bitmap bitmap) {
//                    imageView.setImageBitmap(bitmap);
//                    if (onSuccessCommand != null) {
//                        onSuccessCommand.execute(bitmap);
//                    }
//                }
//            }, UiThreadImmediateExecutorService.getInstance());
//        }
//    }

}

